package com.kensure.shike.bsc.service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import co.kensure.exception.BusinessExceptionUtil;
import co.kensure.exception.ParamUtils;
import co.kensure.frame.JSBaseService;
import co.kensure.mem.CollectionUtils;
import co.kensure.mem.MapUtils;
import co.kensure.mem.PageInfo;

import com.kensure.basekey.BaseKeyService;
import com.kensure.shike.bsc.dao.SKBanShiChuDao;
import com.kensure.shike.bsc.model.SKBanShiChu;
import com.kensure.shike.user.model.SKUser;
import com.kensure.shike.user.service.SKUserService;


/**
 * 办事处服务实现类
 * @author fankd created on 2019-11-4
 * @since 
 */
@Service
public class SKBanShiChuService extends JSBaseService{
	
	@Resource
	private SKBanShiChuDao dao;
	@Resource
	private BaseKeyService baseKeyService;
	@Resource
	private SKUserService sKUserService;
    
    
    public SKBanShiChu selectOne(Long id){
    	return dao.selectOne(id);
    }
	
	public List<SKBanShiChu> selectByIds(Collection<Long> ids){
		return dao.selectByIds(ids);
	}
	
	public List<SKBanShiChu> selectAll(){
		return dao.selectAll();
	}
	
	public List<SKBanShiChu> selectByWhere(Map<String, Object> parameters){
		return dao.selectByWhere(parameters);
	}
	
	
	public long selectCount(){
		return dao.selectCount();
	}
	
	public long selectCountByWhere(Map<String, Object> parameters){
		return dao.selectCountByWhere(parameters);
	}
	
	
	public boolean insert(SKBanShiChu obj){
		super.beforeInsert(obj);
		obj.setId(baseKeyService.getKey("sk_banshichu"));
		return dao.insert(obj);
	}
	
	public boolean update(SKBanShiChu obj){
		super.beforeUpdate(obj);
		return dao.update(obj);
	}
    
    public boolean updateByMap(Map<String, Object> params){
		return dao.updateByMap(params);
	}
    
    
	public boolean delete(Long id){
		return dao.delete(id);
	}	
	
    public boolean deleteMulti(Collection<Long> ids){
		return dao.deleteMulti(ids);
	}
    
    public boolean deleteByWhere(Map<String, Object> parameters){
		return dao.deleteByWhere(parameters);
	}
    
    /**
     * 获取市办事处
     * @param cityname
     * @return
     */
    public SKBanShiChu getShi(String cityname){
    	Map<String, Object> parameters = MapUtils.genMap("title",cityname,"typeid",1);
    	List<SKBanShiChu> list = selectByWhere(parameters);
    	SKBanShiChu bsc = null;
    	if(CollectionUtils.isNotEmpty(list)){
    		bsc = list.get(0);
    	}
    	return bsc;
    }
    
    /**
     * 获取县办事处
     * @param cityname
     * @return
     */
    public SKBanShiChu getXian(String cityname,String xianname){
    	Map<String, Object> parameters = MapUtils.genMap("title",cityname,"title2",xianname,"typeid",2);
    	List<SKBanShiChu> list = selectByWhere(parameters);
    	SKBanShiChu bsc = null;
    	if(CollectionUtils.isNotEmpty(list)){
    		bsc = list.get(0);
    	}
    	return bsc;
    }
    
    
    /**
     * 后端查看接口
     * @param page
     * @return
     */
    public List<SKBanShiChu> selectByWhere(PageInfo page) {
		Map<String, Object> parameters = MapUtils.genMap("orderby", "id desc");
		MapUtils.putPageInfo(parameters, page);
		List<SKBanShiChu> list = selectByWhere(parameters);
		for (SKBanShiChu cs : list) {
			SKUser skuser = sKUserService.selectOne(cs.getUserid());
			cs.setUser(skuser);
		}

		return list;
	}
  
    
    public void add(SKBanShiChu obj) {
		ParamUtils.isBlankThrewException(obj.getTitle(), "名称不能为空");
		ParamUtils.isBlankThrewException(obj.getUserid(), "关联用户不能为空");
		ParamUtils.isBlankThrewException(obj.getTypeid(), "类型不能为空");
		SKUser skuser = sKUserService.selectOne(obj.getUserid());
		if(skuser == null){
			BusinessExceptionUtil.threwException("找不到该用户");
		}
		
		if(obj.getTypeid() == 2 && obj.getTitle2() == null){
			BusinessExceptionUtil.threwException("请选择县");
		}
		if (obj.getId() != null) {
			update(obj);
		} else {
			insert(obj);
		}
	}

}
